/*
 * 文 件    名:  SearchHistoryService.java
 * 创建日期:  2012-10-11上午11:17:19
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  cl073
 */
package com.weipu.common.facade.content.api;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.weipu.common.DXApplication;
import com.weipu.common.facade.content.BaseService;
import com.weipu.common.facade.content.ContentURI;
import com.weipu.common.facade.content.colum.HotWordColumn;
import com.weipu.common.util.Logger;
/**
 * 热搜词汇查询
 * @author Administrator
 *
 */
public class HotWordService extends BaseService
{
    
    /**
      * 自定义条件查询并返回游标
      * @param context
      * @param projection
      * @param selection
      * @param args
      * @param sortOrder
      * @return  [参数说明]
      * @return Cursor [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public Cursor queryBySelection(Context context, String[] projection, String selection, String[] args,
        String sortOrder)
    {
        return context.getContentResolver().query(ContentURI.HotWord.CONTENT_URI_ALL,
            projection,
            selection,
            args,
            sortOrder);
        
    }
    
   /**
    * 保存热搜词汇
    * @param context
    * @param hotWords 词汇源
    * @return
    */
    public long save(Context context, String[] hotWords)
    {
        if (!check(context, hotWords))
        {
            return -1;
        }
        Log.i("FFF", "hotWords "+hotWords.toString()+" "+hotWords.length);
        for(int i=0;i<hotWords.length;i++){
        	ContentValues map = new ContentValues();
        	String hotWord=hotWords[i];
        	 if (null != hotWord)
             {
                 map.put(HotWordColumn.HOT_WORD_ID, 0);
                 map.put(HotWordColumn.HOT_WORD, hotWord);
             }
             try
             {
                 context.getContentResolver().insert(ContentURI.HotWord.CONTENT_URI_ALL, map);
             }
             catch (Exception e)
             {
                 return -1;
             }
        }
        return 0;
    }
    
    /**
     * 查询所有的热搜词汇
     * @param context
     * @param sortOrder
     * @return
     */
    public String[] selectMenusList(Context context, String sortOrder)
    {
    	Log.i("----", context+"  "+HotWordColumn.QUERY_PROJECTION+"  "+sortOrder);
        Cursor cursor = this.queryBySelection(context, HotWordColumn.QUERY_PROJECTION, null, null, sortOrder);
        Logger.d("--cursor--", "--------" + cursor);
        String [] hotWords=null;
        if (cursor == null)
        {
            
        }
        else
        {
            
            cursor.moveToFirst();
            int size=cursor.getCount();
            //数据集合不为空
            if (cursor.getCount() > 0)
            {
            	hotWords=new String[size];
            	int i=0;
                do
                {
                	hotWords[i]=cursor.getString(2);
                			i++;
                    //后移
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
        return hotWords;
    }
    
    /**
	 * 删除所有热搜数据
	 * 
	 */
	public int clearHotWordData() {

		int returnint = DXApplication.getContext().getContentResolver()
				.delete(ContentURI.HotWord.CONTENT_URI_ALL, null, null);
		return returnint;

	}
    
}
